// Package _4_二维数组中的查找 main
// @Title _4_二维数组中的查找
// @Description
// @Author  == 琳琳也叫王木木 ==  2022-01-12 11:07
// @Create  2022-01-12 11:07
package main

import "fmt"

/*
	暴力遍历搜索
*/
func findNumberIn2DArray(matrix [][]int, target int) bool {
	if len(matrix) == 0 {
		return false
	}
	for i := 0; i < len(matrix); i++ {
		for j := 0; j < len(matrix[i]); j++ {
			if matrix[i][j] == target {
				return true
			}
			if matrix[i][j] > target {
				continue
			}
		}
	}
	return false
}

/**
用一点点技术含量，因为数组是递增的
*/
func findNumberIn2DArray2(matrix [][]int, target int) bool {
	if len(matrix) == 0 {
		return false
	}
	j := 0
	i := len(matrix) - 1 // 获取有多少
	for i >= 0 && j < len(matrix[0]) {
		if matrix[i][j] > target {
			i -= 1
		} else if matrix[i][j] < target {
			j += 1
		} else {
			return true
		}
	}
	return false
}

func main() {
	num := [][]int{
		{1, 4, 7, 11, 15},
		{2, 5, 8, 12, 19},
		{3, 6, 9, 16, 22},
		{10, 13, 14, 17, 24},
		{18, 21, 23, 26, 30},
	}
	fmt.Println(findNumberIn2DArray2(num, 9))
}
